package com.nutiteq.tilelayers;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;

import com.nutiteq.BasicMapComponent;
import com.nutiteq.cache.CachingChain;
import com.nutiteq.cache.MemoryCache;
import com.nutiteq.cache.RmsCache;
import com.nutiteq.components.WgsPoint;
import com.nutiteq.controls.ControlKeys;
import com.nutiteq.listeners.MapListener;
import com.nutiteq.maps.GeoMap;
import com.nutiteq.maps.OpenStreetMap;
import com.nutiteq.net.DefaultDownloadStreamOpener;
import com.nutiteq.ui.DefaultCursor;
import com.nutiteq.ui.ThreadDrivenPanning;

public class LayersCanvas extends Canvas implements MapListener, CommandListener {
  private final BasicMapComponent mapComponent;
  private final Command exit;

  public LayersCanvas() {
    mapComponent = new BasicMapComponent("abcdtrial", "Nutiteq", "Map tile layers demo",
        getWidth(), getHeight(), new WgsPoint(-112.264824, 36.097439), 10);
    exit = new Command("Exit", Command.EXIT, 0);
    addCommand(exit);
    setCommandListener(this);
  }

  public void initialize() {
    final GeoMap map = OpenStreetMap.MAPNIK;
    map.addTileOverlay(new ExtraOverlay());
    mapComponent.setMap(map);

    final DefaultDownloadStreamOpener opener = new DefaultDownloadStreamOpener();
    opener
        .addRequestProperty(
            "User-Agent",
            "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.6) Gecko/2009020911 Ubuntu/8.10 (intrepid) Firefox/3.0.6");
    mapComponent.setDownloadStreamOpener(opener);

    mapComponent.setPanningStrategy(new ThreadDrivenPanning());

    final MemoryCache memoryCache = new MemoryCache(50 * 1024);
    final RmsCache rmsCache = new RmsCache("ML_NETWORK_CACHE", 64 * 1024, 5);
    mapComponent.setNetworkCache(new CachingChain(new com.nutiteq.cache.Cache[] { memoryCache,
        rmsCache }));
    mapComponent.setMapListener(this);

    mapComponent.defineControlKey(ControlKeys.MOVE_UP_KEY, -1);
    mapComponent.defineControlKey(ControlKeys.MOVE_DOWN_KEY, -2);
    mapComponent.defineControlKey(ControlKeys.MOVE_LEFT_KEY, -3);
    mapComponent.defineControlKey(ControlKeys.MOVE_RIGHT_KEY, -4);
    mapComponent.defineControlKey(ControlKeys.SELECT_KEY, KEY_NUM5);

    mapComponent.setCursor(new DefaultCursor(0xFFFF0000));

    mapComponent.startMapping();
  }

  protected void paint(final Graphics g) {
    mapComponent.paint(g);
  }

  public void mapClicked(final WgsPoint p) {

  }

  public void mapMoved() {

  }

  public void needRepaint(final boolean mapIsComplete) {
    repaint();
  }

  protected void keyPressed(final int keyCode) {
    mapComponent.keyPressed(keyCode);
  }

  protected void keyReleased(final int keyCode) {
    mapComponent.keyReleased(keyCode);
  }

  protected void keyRepeated(final int keyCode) {
    mapComponent.keyRepeated(keyCode);
  }

  protected void pointerPressed(final int x, final int y) {
    mapComponent.pointerPressed(x, y);
  }

  protected void pointerReleased(final int x, final int y) {
    mapComponent.pointerReleased(x, y);
  }

  protected void pointerDragged(final int x, final int y) {
    mapComponent.pointerDragged(x, y);
  }

  public void commandAction(final Command cmd, final Displayable d) {
    if (cmd == exit) {
      mapComponent.stopMapping();
      TileLayers.instance.notifyDestroyed();
    }
  }
}
